<?php
require_once 'defines.php';
class Asterye_waimaixiaModuleWxapp extends WeModuleWxapp {

	public function __construct() {
	}
	public function doPageOpenid() {
		global $_W,$_GPC;
		load()->func('communication');
		$userinfo2 = ihttp_get("https://api.weixin.qq.com/sns/jscode2session?appid={$_W['account']['key']}&secret={$_W['account']['secret']}&js_code={$_GPC['code']}&grant_type=authorization_code");
		if($userinfo2['status'] != 'OK') {
			$this->result(1,'用户信息', '访问接口失败！');
		}
		$userinfo = json_decode($userinfo2['content'],true);
		$this->result(0,'用户信息', $userinfo);
    }

	public function aesencrypt($data,$key='')
   {
       $base64 = openssl_encrypt($data, 'AES-128-ECB', $key, OPENSSL_RAW_DATA, '');
       return bin2hex($base64);
   }

   public function array_split( $data, $num = 5 )
    {
        
        $arrRet = array();
        if( !isset( $data ) || empty( $data ) )
        {
            return $arrRet;
        }
        
        $iCount = count( $data )/$num;
        if( !is_int( $iCount ) )
        {
            $iCount = ceil( $iCount );
        }
        else
        {
            $iCount += 1;
        }
        for( $i=0; $i<$iCount;++$i )
        {
            $arrInfos = array_slice( $data, $i*$num, $num );
            if( empty( $arrInfos ) )
            {
                continue;
            }
            $arrRet[] = $arrInfos;
            unset( $arrInfos );
        }
        
        return $arrRet;
        
    }

	public function doPagePay()
	{
		global $_GPC;
		global $_W;
		$settings = $this->module['config'];
		$order_id = $_GPC["order_id"];
		$type = $_GPC["type"];
		if ($type == "movie") {
			$detail = pdo_get($this->modulename . "_movieorder", array("userOrderId" => $order_id, "uniacid" => $_W["uniacid"]));
			$params = array("tid" => "movie_" . $detail["userOrderId"], "user" => $detail["openid"], "fee" => floatval(round($detail["orderPrice"]/100,2)), "title" => "购买电影票");
			$_W["openid"] = $detail["openid"];
			$pay_params = $this->pay($params);
			if (is_error($pay_params)) {
				if($settings['errorcode'] == 1){
					$log = pdo_insert($this->modulename . "_systemlog", array("uniacid" => $_W["uniacid"], "api_appKey" => '电影票支付',"apiname" => 'doPagePay',"create_time" => $_W["timestamp"], "log" => json_encode($pay_params)));
				}
				return $this->result(1, "支付失败，请重试");
			}
			return $res = $this->result(0, "", $pay_params);
		}
		if ($type == "movie2") {
			$detail = pdo_get($this->modulename . "_movieorder", array("userOrderId" => $order_id, "uniacid" => $_W["uniacid"]));
			$params = array("tid" => "movie2_" . $detail["userOrderId"], "user" => $detail["openid"], "fee" => floatval(round($detail["orderPrice"]/100,2)), "title" => "购买电影票");
			$_W["openid"] = $detail["openid"];
			$pay_params = $this->pay($params);
			if (is_error($pay_params)) {
				if($settings['errorcode'] == 1){
					$log = pdo_insert($this->modulename . "_systemlog", array("uniacid" => $_W["uniacid"], "api_appKey" => '电影票支付',"apiname" => 'doPagePay',"create_time" => $_W["timestamp"], "log" => json_encode($pay_params)));
				}
				return $this->result(1, "支付失败，请重试");
			}
			return $res = $this->result(0, "", $pay_params);
		}
		if ($type == "item") {
			$detail = pdo_get($this->modulename . "_item_order", array("orderid" => $order_id, "uniacid" => $_W["uniacid"]));
			$item = pdo_get($this->modulename . "_item", array("item_id" => $detail['item_id']));
			if ($item['stock'] == 0) {
				return $this->result(1, "库存不足");
			}
			if($settings['system_type'] == 1){
				$user = pdo_get($this->modulename . "_user_u", array("unionid" => $detail['unionid'], "system_id" => $settings['system_id']));
			}else{
				$user = pdo_get($this->modulename . "_user", array("openid" => $detail['openid'], "uniacid" => $_W["uniacid"]));
			}
			if($detail['price'] > 0){//如果有积分
				if ($user["credit"] < $detail['price']) {
					return $this->result(1, "积分不足");
				}
			}

			$params = array("tid" => "item_" . $order_id, "user" => $detail["openid"], "fee" => floatval(round($detail["market_price"],2)), "title" => "购买商品");
			$_W["openid"] = $detail["openid"];
			$pay_params = $this->pay($params);
			if (is_error($pay_params)) {
				if($settings['errorcode'] == 1){
					$log = pdo_insert($this->modulename . "_systemlog", array("uniacid" => $_W["uniacid"], "api_appKey" => '积分订单支付',"apiname" => 'doPagePay',"create_time" => $_W["timestamp"], "log" => json_encode($pay_params)));
				}
				return $this->result(1, "支付失败，请重试");
			}
			return $res = $this->result(0, "", $pay_params);
		}
	}

	public function payResult($params)
	{
		global $_W;
		$settings = $this->module['config'];
		$uniacid = intval($_W["uniacid"]);
		if ($params["result"] == "success" && $params["from"] == "notify") {
			$tid = $params["tid"];
			$tidarr = explode("_", $tid);
			$order_biz = $tidarr[0];
			$order_id = $tidarr[1];
			if ($order_biz == "movie" || $order_biz == "movie2") {
				$order = pdo_get($this->modulename . "_movieorder", array("userOrderId" => $order_id, "uniacid" => $_W["uniacid"]));
				if (!$order) {
					exit("ERROR");
				}
				if ($order["pay_status"] != 0) {
					exit(json_encode(array('code' => 'SUCCESS', 'message' => '成功')));
				}
				pdo_update($this->modulename . "_movieorder", array("pay_status" => 1, "pay_time" => $params["paytime"], "uniontid" => $params["uniontid"], "trans_id" => $params['tag']["transaction_id"]), array("userOrderId" => $order_id));
				if($settings['movie_ticket_type'] == 1){
					
					//将电影票订单插入总表
					$order['orderPrice'] = round($order['orderPrice']/100,2);
					if($settings['system_type'] == 1){
						$u = pdo_get($this->modulename . "_user_u", array("unionid" => $order["unionid"], "system_id" => $settings['system_id']));
						$fee = $this->fee($u['uid'],$_W["uniacid"]);
						$log = pdo_insert($this->modulename . "_systemlog", array("uniacid" => $_W["uniacid"], "api_appKey" => $settings['movie_api_s_appKey'],"apiname" => 'payResult',"create_time" => $_W["timestamp"], "log" => json_encode($fee)));
						$resin = pdo_insert($this->modulename . "_order_u", array("pingtai" => 'movie' , "pingtai_name" => '电影票', "order_id" => $order_id , "order_paid_time" => $params["paytime"] , "order_price" => $order['orderPrice'] , "order_profit" => 0 , "shopname" => $order['cinemaName'] , "goods_img" => $order['filmPic'] , "goods_title" => $order['filmName'], "order_create_time" => $order['createTime'] , "order_status" => 1, "new" => 1 , "system_id" => $settings['system_id'] , "c_1" => 0 , "c_1_uid" => $fee['c_1_uid'] , "c1_uid" => $fee['c1_uid'] , "c_2" => 0 , "c_2_uid" => $fee['c_2_uid'] , "c_3" => 0 , "c_3_uid" => $fee['c_3_uid'] , "c_t" => 0 , "c_t_uid" => $fee['c_t_uid'] , "c_2t" => 0 , "c_2t_uid" => $fee['c_2t_uid'] , "c_1h" => 0 , "c_1h_uid" => $fee['c_1h_uid'] , "c_2h" => 0 , "c_2h_uid" => $fee['c_2h_uid'] , "c_3h" => 0 , "c_3h_uid" => $fee['c_3h_uid'], "c_4h" => 0 , "c_4h_uid" => $fee['c_4h_uid']));
					}else{
						$u = pdo_get($this->modulename . "_user", array("openid" => $order["openid"], "uniacid" => $_W["uniacid"]));
						$fee = $this->fee($u['uid'],$_W["uniacid"]);
						$resin = pdo_insert($this->modulename . "_order", array("pingtai" => 'movie' , "pingtai_name" => '电影票', "order_id" => $order_id , "order_paid_time" => $params["paytime"] , "order_price" => $order['orderPrice'] , "order_profit" => 0 , "shopname" => $order['cinemaName'] , "goods_img" => $order['filmPic'] , "goods_title" => $order['filmName'], "order_create_time" => $order['createTime'] , "order_status" => 1, "new" => 1 , "uniacid" => $_W["uniacid"] , "c_1" => 0 , "c_1_uid" => $fee['c_1_uid'] , "c1_uid" => $fee['c1_uid'] , "c_2" => 0 , "c_2_uid" => $fee['c_2_uid'] , "c_3" => 0 , "c_3_uid" => $fee['c_3_uid'] , "c_t" => 0 , "c_t_uid" => $fee['c_t_uid'] , "c_2t" => 0 , "c_2t_uid" => $fee['c_2t_uid'] , "c_1h" => 0 , "c_1h_uid" => $fee['c_1h_uid'] , "c_2h" => 0 , "c_2h_uid" => $fee['c_2h_uid'] , "c_3h" => 0 , "c_3h_uid" => $fee['c_3h_uid'], "c_4h" => 0 , "c_4h_uid" => $fee['c_4h_uid']));
					}
					//将电影票订单插入总表
					//发送小程序消息
					$movie_admin = explode(",",$settings['movie_ticket_adminopenid']);
					foreach($movie_admin as $k=>$v){
						$res = $this->xcxnewmovieorderNotify($order,$v);
						if($res){
							if($settings['system_type'] == 1){
								pdo_update($this->modulename . "_user_u", array("movie_tpl_num +=" => -1), array("unionid" => $unionid, "system_id" => $settings['system_id']));
								pdo_update($this->modulename . "_user", array("movie_tpl_num +=" => -1), array("openid" => $openid, "uniacid" => $_W["uniacid"]));
							}else{
								pdo_update($this->modulename . "_user", array("movie_tpl_num +=" => -1), array("openid" => $openid, "uniacid" => $_W["uniacid"]));
							}
						}else{
							if($settings['system_type'] == 1){
								pdo_update($this->modulename . "_user_u", array("movie_tpl_num" => 0), array("unionid" => $unionid, "system_id" => $settings['system_id']));
								pdo_update($this->modulename . "_user", array("movie_tpl_num" => 0), array("openid" => $openid, "uniacid" => $_W["uniacid"]));
							}else{
								pdo_update($this->modulename . "_user", array("movie_tpl_num" => 0), array("openid" => $openid, "uniacid" => $_W["uniacid"]));
							}
						}
					}
				}else{
					//发送订单到第三方
					$time = time();
					$order['seat'] = str_replace("|",",",$order['seat']);
					$data = array("appKey" => $settings['movie_api_s_appKey'], "time" => $time, "showId" => $order['showId'], "seat" => $order['seat'], "seatNo" => $order['seatNo'], "seatId" => $order['seatId'], "reservedPhone" => $order['reservedPhone'], "thirdOrderId" => $order['thirdOrderId'], "acceptChangeSeat" => $order['acceptChangeSeat'], "notifyUrl" => $settings['movie_api_notifyUrl']);
					$log = pdo_insert($this->modulename . "_systemlog", array("uniacid" => $_W["uniacid"], "api_appKey" => $settings['movie_api_s_appKey'],"apiname" => 'api/order/create',"create_time" => $_W["timestamp"], "log" => $data));
					$sign = $this->get_sign($data, $settings['movie_api_s_appSecret']);
					$data["sign"] = $sign;
					if($order_biz == "movie"){
						$url = $settings['movie_api_s_url'] . "api/order/create";
						$res = ihttp_request($url, json_encode($data), array("CURLOPT_HTTPHEADER" => array("Content-Type: application/json;charset=utf-8")));
						$res = json_decode($res["content"], true);
					}else{
						$url = $settings['movie_api_s_url2'] . "api/order/create";
						$res = $this->geturl($url,$data);
					}
					$log = pdo_insert($this->modulename . "_systemlog", array("uniacid" => $_W["uniacid"], "api_appKey" => $settings['movie_api_s_appKey'],"apiname" => 'api/order/create',"create_time" => $_W["timestamp"], "log" => json_encode($res)));
					if($res['code'] == 200){
						//将电影票订单插入总表
						$order['orderPrice'] = round($order['orderPrice']/100,2);
						if($settings['system_type'] == 1){
							$u = pdo_get($this->modulename . "_user_u", array("unionid" => $order["unionid"], "system_id" => $settings['system_id']));
							$fee = $this->fee($u['uid'],$_W["uniacid"]);
							$log = pdo_insert($this->modulename . "_systemlog", array("uniacid" => $_W["uniacid"], "api_appKey" => $settings['movie_api_s_appKey'],"apiname" => 'payResult',"create_time" => $_W["timestamp"], "log" => json_encode($fee)));
							$resin = pdo_insert($this->modulename . "_order_u", array("pingtai" => 'movie' , "pingtai_name" => '电影票', "order_id" => $order_id , "order_paid_time" => $params["paytime"] , "order_price" => $order['orderPrice'] , "order_profit" => 0 , "shopname" => $order['cinemaName'] , "goods_img" => $order['filmPic'] , "goods_title" => $order['filmName'], "order_create_time" => $order['createTime'] , "order_status" => 1, "new" => 1 , "system_id" => $settings['system_id'] , "c_1" => 0 , "c_1_uid" => $fee['c_1_uid'] , "c1_uid" => $fee['c1_uid'] , "c_2" => 0 , "c_2_uid" => $fee['c_2_uid'] , "c_3" => 0 , "c_3_uid" => $fee['c_3_uid'] , "c_t" => 0 , "c_t_uid" => $fee['c_t_uid'] , "c_2t" => 0 , "c_2t_uid" => $fee['c_2t_uid'] , "c_1h" => 0 , "c_1h_uid" => $fee['c_1h_uid'] , "c_2h" => 0 , "c_2h_uid" => $fee['c_2h_uid'] , "c_3h" => 0 , "c_3h_uid" => $fee['c_3h_uid'], "c_4h" => 0 , "c_4h_uid" => $fee['c_4h_uid']));
						}else{
							$u = pdo_get($this->modulename . "_user", array("openid" => $order["openid"], "uniacid" => $_W["uniacid"]));
							$fee = $this->fee($u['uid'],$_W["uniacid"]);
							$resin = pdo_insert($this->modulename . "_order", array("pingtai" => 'movie' , "pingtai_name" => '电影票', "order_id" => $order_id , "order_paid_time" => $params["paytime"] , "order_price" => $order['orderPrice'] , "order_profit" => 0 , "shopname" => $order['cinemaName'] , "goods_img" => $order['filmPic'] , "goods_title" => $order['filmName'], "order_create_time" => $order['createTime'] , "order_status" => 1, "new" => 1 , "uniacid" => $_W["uniacid"] , "c_1" => 0 , "c_1_uid" => $fee['c_1_uid'] , "c1_uid" => $fee['c1_uid'] , "c_2" => 0 , "c_2_uid" => $fee['c_2_uid'] , "c_3" => 0 , "c_3_uid" => $fee['c_3_uid'] , "c_t" => 0 , "c_t_uid" => $fee['c_t_uid'] , "c_2t" => 0 , "c_2t_uid" => $fee['c_2t_uid'] , "c_1h" => 0 , "c_1h_uid" => $fee['c_1h_uid'] , "c_2h" => 0 , "c_2h_uid" => $fee['c_2h_uid'] , "c_3h" => 0 , "c_3h_uid" => $fee['c_3h_uid'], "c_4h" => 0 , "c_4h_uid" => $fee['c_4h_uid']));
						}
						//将电影票订单插入总表
						exit(json_encode(array('code' => 'SUCCESS', 'message' => '成功')));
					}elseif($res['code'] == 101){
						pdo_update($this->modulename . "_movieorder", array("orderStatus" => 0,'orderStatusStr' => '账户余额不足'), array("userOrderId" => $order_id));
					}elseif($settings['errorcode'] == 1){
						$log = pdo_insert($this->modulename . "_systemlog", array("uniacid" => $_W["uniacid"], "api_appKey" => '发送订单',"apiname" => 'api/order/create',"create_time" => $_W["timestamp"], "log" => json_encode($res)));
					}
					//发送订单到第三方
				}
				
				
			}
			if ($order_biz == "item") {
				$detail = pdo_get($this->modulename . "_item_order", array("orderid" => $order_id, "uniacid" => $_W["uniacid"]));
				if($detail['price'] > 0){//如果有积分
					$num = 0 - $detail["price"];
					$pay_type = 3;
					if($settings['system_type'] == 1){
						$res = pdo_update($this->modulename . "_user_u", array('credit +=' => $num), array("unionid" => $detail['unionid'], "system_id" => $settings['system_id']));
						$user = pdo_get($this->modulename . "_user_u", array("unionid" => $detail['unionid'], "system_id" => $settings['system_id']));
					}else{
						$res = pdo_update($this->modulename . "_user", array('credit +=' => $num), array("openid" => $detail['openid'], "uniacid" => $_W["uniacid"]));
						$user = pdo_get($this->modulename . "_user", array("openid" => $detail['openid'], "uniacid" => $_W["uniacid"]));
					}
					$result = pdo_insert($this->modulename . "_user_creditlogs", array("unionid" => $detail['unionid'], "openid" => $detail['openid'], "user_id" => $user['user_id'], "system_id" => $settings['system_id'], "uniacid" => $_W['uniacid'],  "num" => $num,  "last_num" => $user['credit'], "create_time" => $_W["timestamp"], "reason" => '使用'.$detail["price"].'积分兑换商品'));
				}else{
					$pay_type = 2;
				}
				$stock = 0 - 1;
				$res = pdo_update($this->modulename . "_item", array('stock +=' => $stock,'sold +=' => 1), array("item_id" => $detail['item_id']));
				pdo_update($this->modulename . "_item_order", array("pay_status" => 1, "pay_type" => $pay_type, "pay_time" => $params["paytime"], "uniontid" => $params["uniontid"], "trans_id" => $params['tag']["transaction_id"]), array("orderid" => $order_id));
			}
		}
	}


	public function geturl($url,$data){
		$postdata = http_build_query($data);
		//if($url == 'https://yp-api.taototo.cn/yp-api/movie/schedule/schedules-list-tpp'){echo $postdata;}
		
		$options = array(
				'http' => array(
				'method' => 'POST',
				'header' => 'Content-type:application/x-www-form-urlencoded',
				'content' => $postdata,
				'timeout' => 1 * 60 // 超时时间（单位:s）
			  )
			);
		$cxContext = stream_context_create($options);
		$result = file_get_contents($url, false, $cxContext);
		$result = json_decode($result,true);
		return $result;
	}

	public function curlPost($url, $data)
	{
		$jsonStr = json_encode($data);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8", "Content-Length: " . strlen($jsonStr)));
		$response = curl_exec($ch);
		$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);
		return json_decode($response, true);
	}

	
	public function curlOpen($url, $config = array())
    {
		$arr = array('post' => false,'referer' => $url,'cookie' => '', 'useragent' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; customie8)', 'timeout' => 20, 'return' => true, 'proxy' => '', 'userpwd' => '', 'nobody' => false,'header'=>array(),'gzip'=>true,'ssl'=>false,'isupfile'=>false);
		$arr = array_merge($arr, $config);
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
		curl_setopt($ch, CURLOPT_NOBODY, $arr['nobody']);  
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, $arr['useragent']);
		curl_setopt($ch, CURLOPT_REFERER, $arr['referer']);
		curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']);
		curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
		//curl_setopt($ch, CURLOPT_HEADER, true);//获取header
		if($arr['gzip']) curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
		
		if($arr['ssl'])
		{
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		}
		if(!empty($arr['cookie']))
		{
			curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
			curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']); 
		} 
		
		if(!empty($arr['proxy']))
		{
			//curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);  
			curl_setopt ($ch, CURLOPT_PROXY, $arr['proxy']);
			if(!empty($arr['userpwd']))
			{            
				curl_setopt($ch,CURLOPT_PROXYUSERPWD,$arr['userpwd']);
			}        
		}    
		
		//ip比较特殊，用键值表示
		if(!empty($arr['header']['ip']))
		{
			array_push($arr['header'],'X-FORWARDED-FOR:'.$arr['header']['ip'],'CLIENT-IP:'.$arr['header']['ip']);
			unset($arr['header']['ip']);
		}   
		$arr['header'] = array_filter($arr['header']);
		
		if(!empty($arr['header']))
		{
			curl_setopt($ch, CURLOPT_HTTPHEADER, $arr['header']); 
		}

		if ($arr['post'] != false)
		{
			curl_setopt($ch, CURLOPT_POST, true);
			if(is_array($arr['post']) && $arr['isupfile'] === false)
			{
				$post = http_build_query($arr['post']);            
			} 
			else
			{
				$post = $arr['post'];
			}
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		}
		$result = curl_exec($ch);
		curl_close($ch);

		return $result;
    }


	
	public function getdistance($lat1, $lng1, $lat2, $lng2, $len_type = 2, $decimal = 0) {
		$radLat1 = $lat1 * 3.1415926535898 / 180.0;
		$radLat2 = $lat2 * 3.1415926535898 / 180.0;
		$a = $radLat1 - $radLat2;
		$b = ($lng1 * 3.1415926535898 / 180.0) - ($lng2 * 3.1415926535898 / 180.0);
		$s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
		$s = $s * 6378.137;
		$s = round($s * 1000);
		if ($len_type > 1)
		{
		$s /= 1000;
		}
		$ddddecimal = round($s, $decimal);
		$ddddecimal = abs($ddddecimal);
		return $ddddecimal.'km';
	}

	public function aesEn($data, $key)
	{
		$data = base64_encode(openssl_encrypt($data, "AES-128-CBC", $key, OPENSSL_RAW_DATA, $key));
		return $data;
	}
	public function aesDe($data, $key)
	{
		$data = base64_decode($data);
		$str = openssl_decrypt($data, "AES-128-CBC", $key, OPENSSL_RAW_DATA, $key);
		return $str;
	}

	public function imageLiuRound($imgpath)
	{
		$ext     = pathinfo($imgpath);
        $src_img = null;
        switch ($ext['extension']) {
        case 'jpg':
            $src_img = imagecreatefromjpeg($imgpath);
            break;
        case 'png':
            $src_img = imagecreatefrompng($imgpath);
            break;
        }
        $wh  = getimagesize($imgpath);
        $w   = $wh[0];
        $h   = $wh[1];
        $w   = min($w, $h);
        $h   = $w;
        $img = imagecreatetruecolor($w, $h);
        //这一句一定要有
        imagesavealpha($img, true);
        //拾取一个完全透明的颜色,最后一个参数127为全透明
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
        imagefill($img, 0, 0, $bg);
        $r   = $w / 2; //圆半径
        $y_x = $r; //圆心X坐标
        $y_y = $r; //圆心Y坐标
        for ($x = 0; $x < $w; $x++) {
            for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($src_img, $x, $y);
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                    imagesetpixel($img, $x, $y, $rgbColor);
                }
            }
        }
        return $img;
	}

    public function getDistance2($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2)
	{

		$EARTH_RADIUS = 6370.996; // 地球半径系数
		$PI           = 3.1415926535898;

		$radLat1 = $lat1 * $PI / 180.0;
		$radLat2 = $lat2 * $PI / 180.0;

		$radLng1 = $lng1 * $PI / 180.0;
		$radLng2 = $lng2 * $PI / 180.0;

		$a = $radLat1 - $radLat2;
		$b = $radLng1 - $radLng2;

		$distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
		$distance = $distance * $EARTH_RADIUS * 1000;

		if ($unit === 2) {
			$distance /= 1000;
		}

		return round($distance, $decimal);
	}

	public function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') 
	{ 
		if($code == 'UTF-8') 
		{ 
			$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; 
			preg_match_all($pa, $string, $t_string);
			if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen));
			return join('', array_slice($t_string[0], $start, $sublen));
		}
		else
		{
			$start = $start*2;
			$sublen = $sublen*2;
			$strlen = strlen($string);
			$tmpstr = '';
			for($i=0; $i< $strlen; $i++)
			{
				if($i>=$start && $i< ($start+$sublen))
				{
					if(ord(substr($string, $i, 1))>129)
					{
						$tmpstr.= substr($string, $i, 2);
					}
					else
					{
						$tmpstr.= substr($string, $i, 1);
					}
				}
				if(ord(substr($string, $i, 1))>129) $i++;
			}
			return $tmpstr;
		}
	}
	public function  meituansign($url,$meituanapikey){

		$signKey = $meituanapikey;
		$url = substr($url,strpos($url,"?")+1);
		$urlarr=explode("&", $url);
		foreach ($urlarr as $key => $value) {
		    $urlarr[$key] = explode("=", $value);
		    if($urlarr[$key][1]==""){
		        unset($urlarr[$key]);//删除空参
		    }else{
		        $urlarr[$key]=implode($urlarr[$key],"");
		    }
		}
		sort($urlarr);//重新排序
		$sring=$signKey.implode($urlarr,"");
		return sha1($sring);//获得签名
	}
	public function zhouji($str_in){
		$str_today = date('Y-m-d'); //获取今天的日期 字符串 
		$ux_today =  strtotime($str_today); //将今天的日期字符串转换为 时间戳
		
		$ux_tomorrow = $ux_today+3600*24;// 获取明天的时间戳
		$str_tomorrow = date('Y-m-d',$ux_tomorrow);//获取明天的日期 字符串
		
		
		$ux_afftertomorrow = $ux_today+3600*24*2;// 获取后天的时间戳
		$str_afftertomorrow = date('Y-m-d',$ux_afftertomorrow);//获取后天的日期 字符串
		
		$ux_in = strtotime($str_in);//获取输入日期的 时间戳
		$str_in_format = date('Y-m-d',$ux_in);//格式化为y-m-d的 日期字符串
    
		if($str_in_format==$str_today){
		   return "今天";
		}else if($str_in_format==$str_tomorrow){
		   return "明天";
		}else if($str_in_format==$str_afftertomorrow){
		   return "后天";
		}else{
			$week=date("N",$ux_in);
			switch ($week) {
			case 1:
			    return "周一";
			    break;
			case 2:
			    return "周二";
			    break;
			case 3:
			    return "周三";
			    break;
			case 4:
			    return "周四";
			    break;
			case 5:
			    return "周五";
			    break;
			case 6:
			    return "周六";
			    break;
			default:
			    return "周日";
			    break;
			}
		}
	}
	public function newuserNotify($openid, $nickname = "")
	{
		global $_GPC;
		global $_W;
		$settings = pdo_get("uni_account_modules", array("module" => $this->modulename , "uniacid" =>$_W["uniacid"]));
        $config = unserialize($settings['settings']);
		load()->model('mc');
		$account_api = WeAccount::create();
		$token = $account_api->getAccessToken();
		$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" . $token;
		
		$post = array();
		$post["touser"] = $openid;
		$mp_template_msg = array("appid" => $config["gzh_appid"], "template_id" => $config["gzh_newuser_tplid"], "miniprogram" => array("appid" => $_W["account"]["key"], "pagepath" => "pages/user/index"), "data" => array("first" => array("value" => "有新用户成为你的粉丝。", "color" => "#173177"), "keyword1" => array("value" => '会员编号：'.$nickname, "color" => "#173177"), "keyword2" => array("value" => '******', "color" => "#173177"), "keyword3" => array("value" => date("Y-m-d H:i:s"), "color" => "#173177"), "remark" => array("value" => "点击本消息可以快速打开小程序。", "color" => "#173177")));
		$post["mp_template_msg"] = $mp_template_msg;
		$res = ihttp_request($url, json_encode($post));
		$res = json_decode($res["content"],true);
		if($res['errcode'] == 0){
			return true;
		}else{
			$log = pdo_insert($this->modulename . "_systemlog", array("uniacid" => $_W["uniacid"], "api_appKey" => '公众号新粉丝通知',"apiname" => 'newuserNotify',"create_time" => $_W["timestamp"], "log" => json_encode($post).json_encode($res)));
			return false;
		}
	}
	public function xcxnewmovieorderNotify($order,$openid)
	{
		global $_GPC;
		global $_W;
		$settings = pdo_get("uni_account_modules", array("module" => $this->modulename , "uniacid" =>$_W["uniacid"]));
        $config = unserialize($settings['settings']);
		$account_api = WeAccount::create();
		$token = $account_api->getAccessToken();
		$url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
		$dd = array();
		$dd['touser'] = $openid;
		$dd['template_id'] = $config["movie_tpl_xcxdaibaojia"];
		//$dd['page'] = 'pages/kefu/webview?return_url='.urlencode($_W['siteroot'].'app/index.php?i='.$_W["uniacid"].'&c=entry&do=movie_order&m='.$this->modulename);
		$dd['page'] = 'pagesA/pages/userA/movieorderdetail?id='.$order['id'];
		$dd['data'] = array(
			"thing1"=>array("value"=>$order['cinemaName']),
			'thing2'=>array("value"=>$order['filmName']),
			'time3'=>array("value"=>$order['showTime']),
			'thing4'=>array("value"=>mb_substr($order['cinemaAddress'],0,20)),
		);
		load()->func('communication');
		$res = ihttp_request($url,json_encode($dd));
		$res = json_decode($res["content"],true);
		$log = pdo_insert($this->modulename . "_systemlog", array("uniacid" => $_W["uniacid"], "api_appKey" => '小程序电影票报价通知',"apiname" => 'xcxnewmovieorderNotify',"create_time" => $_W["timestamp"], "log" => json_encode($dd).json_encode($res)));
		if($res['errcode'] == 0){
			return true;
		}else{
			return false;
		}
	}
	public function xcxnewuserNotify($openid, $nickname = "")
	{
		global $_GPC;
		global $_W;
		$settings = pdo_get("uni_account_modules", array("module" => $this->modulename , "uniacid" =>$_W["uniacid"]));
        $config = unserialize($settings['settings']);
		$account_api = WeAccount::create();
		$token = $account_api->getAccessToken();
		$url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
		$dd = array();
		$dd['touser'] = $openid;
		$dd['template_id'] = $config["warn_hy_tplid"];
		$dd['page'] = 'pages/user/index';
		$dd['data'] = array(
			 $config["warn_hy_tplid_1"]=>array(
				"value"=>'会员编号：'.$nickname
			),
			$config["warn_hy_tplid_2"]=>array(
				"value"=>date("Y-m-d H:i:s")
			),
		);
		load()->func('communication');
		$res = ihttp_request($url,json_encode($dd));
		if($settings['errorcodeid'] == 87){
			var_dump($res);
		}
		$res = json_decode($res["content"],true);
		$log = pdo_insert($this->modulename . "_systemlog", array("uniacid" => $_W["uniacid"], "api_appKey" => '小程序新粉丝通知',"apiname" => 'xcxnewuserNotify',"create_time" => $_W["timestamp"], "log" => json_encode($dd).json_encode($res)));
		if($res['errcode'] == 0){
			return true;
		}else{
			return false;
		}
	}
	public function xcxneworderNotify($openid, $nickname = "")
	{
		global $_GPC;
		global $_W;
		$settings = pdo_get("uni_account_modules", array("module" => $this->modulename , "uniacid" =>$_W["uniacid"]));
        $config = unserialize($settings['settings']);
		$account_api = WeAccount::create();
		$token = $account_api->getAccessToken();
		$url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
		$dd = array();
		$dd['touser'] = $openid;
		$dd['template_id'] = $config["warn_hy_tplid"];
		$dd['page'] = 'pages/index/index?to=user';
		$dd['data'] = array(
			"thing1"=>array(
				"value"=>'编号：'.$nickname
			),
			"time2"=>array(
				"value"=>date("Y-m-d H:i:s")
			),
		);
		load()->func('communication');
		$res = ihttp_request($url,json_encode($dd));
		$res = json_decode($res["content"],true);
		$log = pdo_insert($this->modulename . "_systemlog", array("uniacid" => $_W["uniacid"], "api_appKey" => '小程序新订单通知',"apiname" => 'xcxneworderNotify',"create_time" => $_W["timestamp"], "log" => json_encode($dd).json_encode($res)));
		if($res['errcode'] == 0){
			return true;
		}else{
			return false;
		}
	}
	
	public function neworderNotify($openid, $order_id, $goods_title)
	{
		global $_GPC;
		global $_W;
		$settings = pdo_get("uni_account_modules", array("module" => $this->modulename , "uniacid" =>$_W["uniacid"]));
        $config = unserialize($settings['settings']);
		load()->model('mc');
		$account_api = WeAccount::create();
		$token = $account_api->getAccessToken();
		$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" . $token;
		
		$post = array();
		$post["touser"] = $openid;
		$mp_template_msg = array("appid" => $config["tmpl_appid"], "template_id" => $config["gzh_neworder_tplid"], "miniprogram" => array("appid" => $_W["account"]["key"], "pagepath" => "pages/index/index?to=user"), "data" => array("first" => array("value" => "又有新订单啦！", "color" => "#173177"), "keyword1" => array("value" => $goods_title, "color" => "#173177"), "keyword2" => array("value" => date("Y-m-d H:i"), "color" => "#173177"), "keyword3" => array("value" => $order_id, "color" => "#173177"), "keyword4" => array("value" => $order_id, "color" => "#173177"), "keyword5" => array("value" => "-", "color" => "#173177"), "remark" => array("value" => "点击本消息可以快速打开小程序。", "color" => "#173177")));
		$post["mp_template_msg"] = $mp_template_msg;
		$res = ihttp_request($url, json_encode($post));
		$res = json_decode($res["content"],true);
		$log = pdo_insert($this->modulename . "_systemlog", array("uniacid" => $_W["uniacid"], "api_appKey" => '公众号新订单通知',"apiname" => 'neworderNotify',"create_time" => $_W["timestamp"], "log" => json_encode($post).json_encode($res)));
		if($res['errcode'] == 0){
			return true;
		}else{
			return false;
		}
	}
	public function genSign($params,$secret)
	{
		unset($params["sign"]);
		ksort($params);
		$str = $secret; // $secret为分配的密钥
		foreach($params as $key => $value) {
			$str .= $key . $value;
		}
		$str .= $secret;
		$sign = md5($str);
		return $sign;
	}
	public function filterEmoji($str)
	{
		$str = preg_replace_callback(
				'/./u',
				function (array $match) {
					return strlen($match[0]) >= 4 ? '' : $match[0];
				},
				$str);
		 return $str;
	 }
	 public function get_sign($params, $secret)
	{
		$string = "";
		ksort($params);
		foreach ($params as $k => $v) {
			if ($k != "sign" && !is_array($v)) {
				$string .= $k . "=" . $v . "&";
			}
		}
		$string = trim($string, "&");
		$string = $string . "&appSecret=".$secret;
		$string = md5($string);
		return $string;
	}

	public function search($all, $keyword)
	{
		$arr = $result = array();
		foreach ($all as $key => $value) {
			if (strstr($value["name"], $keyword) !== false) {
				array_push($arr, $key);
			}
		}
		foreach ($arr as $key => $value) {
			if (array_key_exists($value, $all)) {
				array_push($result, $all[$value]);
			}
		}
		return $result;
	}
	public function cinemasearch($all, $keyword)
	{
		$arr = $result = array();
		foreach ($all as $key => $value) {
			if (strstr($value["cinemaName"], $keyword) !== false) {
				array_push($arr, $key);
			}
		}
		foreach ($arr as $key => $value) {
			if (array_key_exists($value, $all)) {
				array_push($result, $all[$value]);
			}
		}
		return $result;
	}

	public function fee($uid,$id)
	{
		global $_GPC;
		global $_W;
		$settings = pdo_get("uni_account_modules", array("module" => $this->modulename , "uniacid" =>$_W['uniacid']));
		$settings = unserialize($settings['settings']);
		if($settings['system_type'] == 1){
			$u = pdo_get($this->modulename."_user_u", array("uid" => $uid, "system_id" => $settings['system_id']));
		}else{
			$u = pdo_get($this->modulename."_user", array("uid" => $uid , "uniacid" => $id));
		}
		if($u){
			//如果是普通会员
			$t = 0;$h = 0;$h1 = 0;$h2 = 0;$h3 = 0;$h4 = 0;
			if($u['is_tuan'] == 0){
				$c_1_uid = $u['uid'];
				$c_1_openid = $u['openid'];
				$c_1_s1 = $u['s_1_num'];
				$c_1_fee = $settings['c_1'];
				if($u['invite_uid'] != '0'){
					$fa_uid_array = array_reverse(explode("_",$u['invite_uid']));
					$i1 = 1;
					foreach($fa_uid_array as $v){
						if($settings['system_type'] == 1){
							$u1 = pdo_get($this->modulename."_user_u", array("uid" => $v , "system_id" => $settings['system_id']));
						}else{
							$u1 = pdo_get($this->modulename."_user", array("uid" => $v , "uniacid" => $id));
						}
						if($u1['is_tuan'] == 0 && $i1 == 1){//如果是普通会员直推
							$c_2_uid = $u1['uid'];
							$c_2_openid = $u1['openid'];
							$c_2_s1 = $u1['s_1_num'];
							$c_2_fee = $settings['c_2'];
							$i2 = 1;
							foreach($fa_uid_array as $v2){
								if($settings['system_type'] == 1){
									$u2 = pdo_get($this->modulename."_user_u", array("uid" => $v2 , "system_id" => $settings['system_id']));
								}else{
									$u2 = pdo_get($this->modulename."_user", array("uid" => $v2 , "uniacid" => $id));
								}
								if($u2['is_tuan'] == 0 && $i2 == 2){//如果是普通会员间推
									$c_3_uid = $u2['uid'];
									$c_3_openid = $u2['openid'];
									$c_3_s1 = $u2['s_1_num'];
									$c_3_fee = $settings['c_3'];
									$i3 = 1;
									foreach($fa_uid_array as $v3){
										if($settings['system_type'] == 1 && $i3 > 2){
											$u3 = pdo_get($this->modulename."_user_u", array("uid" => $v3 , "system_id" => $settings['system_id']));
										}elseif($i3 > 2){
											$u3 = pdo_get($this->modulename."_user", array("uid" => $v3 , "uniacid" => $id));
										}
										if($u3['is_tuan'] == 1 && $t == 0 && $h == 0 && $h1 == 0 && $i3 > 2){//团长非直属
											$c_t_uid = $u3['uid'];
											$c_t_openid = $u3['openid'];
											$c_t_s1 = $u3['s_1_num'];
											$c_t_fee = $settings['c_t'];
											$t = 1;
											continue;
										}elseif($u3['is_tuan'] == 1 && $t == 1 && $h == 0 && $h1 == 0  && $i3 > 2){//团长直属团长
											$c_2t_uid = $u3['uid'];
											$c_2t_openid = $u3['openid'];
											$c_2t_s1 = $u3['s_1_num'];
											$c_2t_fee = $settings['c_2t'];
											$t = 2;
											continue;
										}elseif($u3['is_tuan'] == 2 && $t == 0 && $h == 0 && $h1 == 0 && $i3 > 2){//合伙人非直属会员
											$c_t_uid = $u3['uid'];
											$c_t_openid = $u3['openid'];
											$c_t_s1 = $u3['s_1_num'];
											$c_t_fee = $settings['c_h_3'];
											$h = 2;
											continue;
										}elseif($u3['is_tuan'] == 2 && $t > 0 && $h == 0 && $h1 == 0 && $i3 > 2){//合伙人直属团队
											$c_1h_uid = $u3['uid'];
											$c_1h_openid = $u3['openid'];
											$c_1h_s1 = $u3['s_1_num'];
											$c_1h_fee = $settings['c_1h'];
											$h = 1;
											continue;
										}elseif($u3['is_tuan'] == 2 && $t > 0 && $h == 1 && $h1 == 0 && $i3 > 2){//合伙人间属团队
											$c_2h_uid = $u3['uid'];
											$c_2h_openid = $u3['openid'];
											$c_2h_s1 = $u3['s_1_num'];
											$c_2h_fee = $settings['c_2h'];
											$h = 2;
											continue;
										}elseif($u3['is_tuan'] == 2 && $h == 2 && $h1 == 0 && $i3 > 2){//合伙人直属合伙人
											$c_3h_uid = $u3['uid'];
											$c_3h_openid = $u3['openid'];
											$c_3h_s1 = $u3['s_1_num'];
											$c_3h_fee = $settings['c_3h'];
											$h1 = 1;
											continue;
										}elseif($u3['is_tuan'] == 2 && $h == 2 && $h1 == 1 && $i3 > 2){//合伙人间属合伙人
											$c_4h_uid = $u3['uid'];
											$c_4h_openid = $u3['openid'];
											$c_4h_s1 = $u3['s_1_num'];
											$c_4h_fee = $settings['c_4h'];
											$h1 = 2;
											break;
										}
										$i3++;
									}
									break;
								}elseif($u2['is_tuan'] == 1 && $i2 == 2){//如果是团长间推
									$c_t_uid = $u2['uid'];
									$c_t_openid = $u2['openid'];
									$c_t_s1 = $u2['s_1_num'];
									$c_t_fee = $settings['c_t'];
									$i3 = 1;
									foreach($fa_uid_array as $v3){
										if($settings['system_type'] == 1 && $i3 > 2){
											$u3 = pdo_get($this->modulename."_user_u", array("uid" => $v3 , "system_id" => $settings['system_id']));
										}elseif($i3 > 2){
											$u3 = pdo_get($this->modulename."_user", array("uid" => $v3 , "uniacid" => $id));
										}
										if($u3['is_tuan'] == 1 && $t == 0 && $h == 0 && $h1 == 0  && $i3 > 2){//团长直属团长
											$c_2t_uid = $u3['uid'];
											$c_2t_openid = $u3['openid'];
											$c_2t_s1 = $u3['s_1_num'];
											$c_2t_fee = $settings['c_2t'];
											$t = 2;
											continue;
										}elseif($u3['is_tuan'] == 1 && $t == 0 && $h == 0 && $h1 == 0  && $i3 > 2){//团长直属团长
											$c_1h_uid = $u3['uid'];
											$c_1h_openid = $u3['openid'];
											$c_1h_s1 = $u3['s_1_num'];
											$c_1h_fee = $settings['c_1h'];
											$h = 1;
											continue;
										}elseif($u3['is_tuan'] == 2 && $h == 0 && $h1 == 0 && $i3 > 2){//合伙人直属团队
											$c_1h_uid = $u3['uid'];
											$c_1h_openid = $u3['openid'];
											$c_1h_s1 = $u3['s_1_num'];
											$c_1h_fee = $settings['c_1h'];
											$h = 1;
											continue;
										}elseif($u3['is_tuan'] == 2 && $h == 1 && $h1 == 0 && $i3 > 2){//合伙人间属团队
											$c_2h_uid = $u3['uid'];
											$c_2h_openid = $u3['openid'];
											$c_2h_s1 = $u3['s_1_num'];
											$c_2h_fee = $settings['c_2h'];
											$h = 2;
											continue;
										}elseif($u3['is_tuan'] == 2 && $h == 2 && $h1 == 0 && $i3 > 2){//合伙人直属合伙人
											$c_3h_uid = $u3['uid'];
											$c_3h_openid = $u3['openid'];
											$c_3h_s1 = $u3['s_1_num'];
											$c_3h_fee = $settings['c_3h'];
											$h1 = 1;
											continue;
										}elseif($u3['is_tuan'] == 2 && $h == 2 && $h1 == 1 && $i3 > 2){//合伙人间属合伙人
											$c_4h_uid = $u3['uid'];
											$c_4h_openid = $u3['openid'];
											$c_4h_s1 = $u3['s_1_num'];
											$c_4h_fee = $settings['c_4h'];
											$h1 = 2;
											break;
										}
										$i3++;
									}
									break;
								}elseif($u2['is_tuan'] == 2 && $i2 == 2){//如果是合伙人间推
									$c_t_uid = $u2['uid'];
									$c_t_openid = $u2['openid'];
									$c_t_s1 = $u2['s_1_num'];
									$c_t_fee = $settings['c_h_3'];
									$i3 = 1;
									foreach($fa_uid_array as $v3){
										if($settings['system_type'] == 1 && $i3 > 2){
											$u3 = pdo_get($this->modulename."_user_u", array("uid" => $v3 , "system_id" => $settings['system_id']));
										}elseif($i3 > 2){
											$u3 = pdo_get($this->modulename."_user", array("uid" => $v3 , "uniacid" => $id));
										}
										if($u3['is_tuan'] == 2 && $h1 == 0 && $i3 > 2){//合伙人直属合伙人
											$c_3h_uid = $u3['uid'];
											$c_3h_openid = $u3['openid'];
											$c_3h_s1 = $u3['s_1_num'];
											$c_3h_fee = $settings['c_3h'];
											$h1 = 1;
											continue;;
										}
										if($u3['is_tuan'] == 2 && $h1 == 1 && $i3 > 2){//合伙人间属合伙人
											$c_4h_uid = $u3['uid'];
											$c_4h_openid = $u3['openid'];
											$c_4h_s1 = $u3['s_1_num'];
											$c_4h_fee = $settings['c_4h'];
											$h1 = 2;
											break;
										}
										$i3++;
									}
									break;
								}
								$i2++;
							}
							break;
						}elseif($u1['is_tuan'] == 1 && $i1 == 1){//如果是团长直推
							$c_2_uid = $u1['uid'];
							$c_2_openid = $u1['openid'];
							$c_2_s1 = $u1['s_1_num'];
							$c_2_fee = $settings['c_t_2'];
							$i2 = 1;
							foreach($fa_uid_array as $v2){
								if($settings['system_type'] == 1){
									$u2 = pdo_get($this->modulename."_user_u", array("uid" => $v2 , "system_id" => $settings['system_id']));
								}else{
									$u2 = pdo_get($this->modulename."_user", array("uid" => $v2 , "uniacid" => $id));
								}
								if($u2['is_tuan'] == 1 && $t == 0  && $h == 0 && $h1 == 0 && $i2 > 1){//如果是团长间推
									$c_2t_uid = $u2['uid'];
									$c_2t_openid = $u2['openid'];
									$c_2t_s1 = $u2['s_1_num'];
									$c_2t_fee = $settings['c_2t'];
									$t = 2;
									continue;
								}elseif($u2['is_tuan'] == 2 && $t == 0  && $h == 0 && $h1 == 0 && $i2 > 1){//如果是团长间推
									$c_1h_uid = $u2['uid'];
									$c_1h_openid = $u2['openid'];
									$c_1h_s1 = $u2['s_1_num'];
									$c_1h_fee = $settings['c_1h'];
									$h = 1;
									continue;
								}elseif($u2['is_tuan'] == 2 && $h == 0 && $h1 == 0 && $i2 > 1){//合伙人直属团队
									$c_1h_uid = $u2['uid'];
									$c_1h_openid = $u2['openid'];
									$c_1h_s1 = $u2['s_1_num'];
									$c_1h_fee = $settings['c_1h'];
									$h = 1;
									continue;
								}elseif($u2['is_tuan'] == 2 && $h == 1 && $h1 == 0 && $i2 > 1){//合伙人间属团队
									$c_2h_uid = $u2['uid'];
									$c_2h_openid = $u2['openid'];
									$c_2h_s1 = $u2['s_1_num'];
									$c_2h_fee = $settings['c_2h'];
									$h = 2;
									continue;
								}elseif($u2['is_tuan'] == 2 && $h == 2 && $h1 == 0 && $i2 > 1){//合伙人直属合伙人
									$c_3h_uid = $u2['uid'];
									$c_3h_openid = $u2['openid'];
									$c_3h_s1 = $u2['s_1_num'];
									$c_3h_fee = $settings['c_3h'];
									$h1 = 1;
									continue;
								}elseif($u2['is_tuan'] == 2 && $h == 2 && $h1 == 1 && $i2 > 1){//合伙人间属合伙人
									$c_4h_uid = $u2['uid'];
									$c_4h_openid = $u2['openid'];
									$c_4h_s1 = $u2['s_1_num'];
									$c_4h_fee = $settings['c_4h'];
									$h1 = 2;
									break;
								}
								$i2++;
							}
							break;
						}elseif($u1['is_tuan'] == 2 && $i1 == 1){//如果是合伙人直推
							$c_2_uid = $u1['uid'];
							$c_2_openid = $u1['openid'];
							$c_2_s1 = $u1['s_1_num'];
							$c_2_fee = $settings['c_h_2'];
							$i2 = 1;
							foreach($fa_uid_array as $v2){
								if($settings['system_type'] == 1 && $i2 > 1){
									$u2 = pdo_get($this->modulename."_user_u", array("uid" => $v2 , "system_id" => $settings['system_id']));
								}elseif($i2 > 1){
									$u2 = pdo_get($this->modulename."_user", array("uid" => $v2 , "uniacid" => $id));
								}
								if($u2['is_tuan'] == 2 && $h1 == 0 && $i2 > 1){//合伙人直属合伙人
									$c_3h_uid = $u2['uid'];
									$c_3h_openid = $u2['openid'];
									$c_3h_s1 = $u2['s_1_num'];
									$c_3h_fee = $settings['c_3h'];
									$h1 = 1;
									continue;
								}elseif($u2['is_tuan'] == 2 && $h1 == 1 && $i2 > 1){//合伙人间属合伙人
									$c_4h_uid = $u2['uid'];
									$c_4h_openid = $u2['openid'];
									$c_4h_s1 = $u2['s_1_num'];
									$c_4h_fee = $settings['c_4h'];
									$h1 = 2;
									break;
								}
								$i2++;
							}
							break;
						}
						$i1++;
					}
				}
			}elseif($u['is_tuan'] == 1){//如果是团长
				$c_1_uid = $u['uid'];
				$c_1_openid = $u['openid'];
				$c_1_s1 = $u['s_1_num'];
				$c_1_fee = $settings['c_t_1'];
				if($u['invite_uid'] != '0'){
					$fa_uid_array = array_reverse(explode("_",$u['invite_uid']));
					$i1 = 1;
					foreach($fa_uid_array as $v){
						if($settings['system_type'] == 1){
							$u1 = pdo_get($this->modulename."_user_u", array("uid" => $v , "system_id" => $settings['system_id']));
						}else{
							$u1 = pdo_get($this->modulename."_user", array("uid" => $v , "uniacid" => $id));
						}
						if($u1['is_tuan'] == 1 && $t == 0 && $h == 0 && $h1 == 0 && $i1 == 1){//如果是团长直推
							$c_2t_uid = $u1['uid'];
							$c_2t_openid = $u1['openid'];
							$c_2t_s1 = $u1['s_1_num'];
							$c_2t_fee = $settings['c_2t'];
							$t = 2;
							continue;
						}elseif($u1['is_tuan'] == 2 && $t == 0 && $h == 0 && $h1 == 0 && $i1 == 1){//如果是合伙人直推
							$c_1h_uid = $u1['uid'];
							$c_1h_openid = $u1['openid'];
							$c_1h_s1 = $u1['s_1_num'];
							$c_1h_fee = $settings['c_1h'];
							$h = 1;
							continue;
						}elseif($u1['is_tuan'] == 2 && $h == 0 && $h1 == 0 && $i1 > 1){//合伙人直属团队
							$c_1h_uid = $u1['uid'];
							$c_1h_openid = $u1['openid'];
							$c_1h_s1 = $u1['s_1_num'];
							$c_1h_fee = $settings['c_1h'];
							$h = 1;
							continue;
						}elseif($u1['is_tuan'] == 2 && $h == 1 && $h1 == 0 && $i1 > 1){//合伙人间属团队
							$c_2h_uid = $u1['uid'];
							$c_2h_openid = $u1['openid'];
							$c_2h_s1 = $u1['s_1_num'];
							$c_2h_fee = $settings['c_2h'];
							$h = 2;
							continue;
						}elseif($u1['is_tuan'] == 2 && $h == 2 && $h1 == 0 && $i1 > 1){//合伙人直属合伙人
							$c_3h_uid = $u1['uid'];
							$c_3h_openid = $u1['openid'];
							$c_3h_s1 = $u1['s_1_num'];
							$c_3h_fee = $settings['c_3h'];
							$h1 = 1;
							continue;
						}elseif($u1['is_tuan'] == 2 && $h == 2 && $h1 == 1 && $i1 > 1){//合伙人间属合伙人
							$c_4h_uid = $u1['uid'];
							$c_4h_openid = $u1['openid'];
							$c_4h_s1 = $u1['s_1_num'];
							$c_4h_fee = $settings['c_4h'];
							$h1 = 2;
							break;
						}
						$i1++;
					}
				}
			}elseif($u['is_tuan'] == 2){//如果是合伙人
				$c_1_uid = $u['uid'];
				$c_1_openid = $u['openid'];
				$c_1_s1 = $u['s_1_num'];
				$c_1_fee = $settings['c_h_1'];
				if($u['invite_uid'] != '0'){
					$fa_uid_array = array_reverse(explode("_",$u['invite_uid']));
					$i1 = 1;
					foreach($fa_uid_array as $v){
						if($settings['system_type'] == 1){
							$u1 = pdo_get($this->modulename."_user_u", array("uid" => $v , "system_id" => $settings['system_id']));
						}else{
							$u1 = pdo_get($this->modulename."_user", array("uid" => $v , "uniacid" => $id));
						}
						if($u1['is_tuan'] == 2 && $h1 == 0 && $i1 > 0){//合伙人直属合伙人
							$c_3h_uid = $u1['uid'];
							$c_3h_openid = $u1['openid'];
							$c_3h_s1 = $u1['s_1_num'];
							$c_3h_fee = $settings['c_3h'];
							$h1 = 1;
							continue;
						}elseif($u1['is_tuan'] == 2 && $h1 == 1 && $i1 > 0){//合伙人间属合伙人
							$c_4h_uid = $u1['uid'];
							$c_4h_openid = $u1['openid'];
							$c_4h_s1 = $u1['s_1_num'];
							$c_4h_fee = $settings['c_4h'];
							$h1 = 2;
							break;
						}
						$i1++;
					}
				}
			}
		}
		$c1_uid = $c_1_uid;
		if(!$c_1_uid || !$c_1_fee){
			$c_1_uid = 0;
			$c_1_openid = '';
			$c_1_s1 = 0;
			$c_1_fee = 0;
		}
		if(!$c_2_uid || !$c_2_fee){
			$c_2_uid = 0;
			$c_2_openid = '';
			$c_2_s1 = 0;
			$c_2_fee = 0;
		}
		if(!$c_3_uid || !$c_3_fee){
			$c_3_uid = 0;
			$c_3_openid = '';
			$c_3_s1 = 0;
			$c_3_fee = 0;
		}
		if(!$c_t_uid || !$c_t_fee){
			$c_t_uid = 0;
			$c_t_openid = '';
			$c_t_s1 = 0;
			$c_t_fee = 0;
		}
		if(!$c_2t_uid || !$c_2t_fee){
			$c_2t_uid = 0;
			$c_2t_openid = '';
			$c_2t_s1 = 0;
			$c_2t_fee = 0;
		}
		if(!$c_1h_uid || !$c_1h_fee){
			$c_1h_uid = 0;
			$c_1h_openid = '';
			$c_1h_s1 = 0;
			$c_1h_fee = 0;
		}
		if(!$c_2h_uid || !$c_2h_fee){
			$c_2h_uid = 0;
			$c_2h_openid = '';
			$c_2h_s1 = 0;
			$c_2h_fee = 0;
		}
		if(!$c_3h_uid || !$c_3h_fee){
			$c_3h_uid = 0;
			$c_3h_openid = '';
			$c_3h_s1 = 0;
			$c_3h_fee = 0;
		}
		if(!$c_4h_uid || !$c_4h_fee){
			$c_4h_uid = 0;
			$c_4h_openid = '';
			$c_4h_s1 = 0;
			$c_4h_fee = 0;
		}
		$fee = array(
			'c1_uid' => $c1_uid,
			'c_1_uid' => $c_1_uid,
			'c_1_openid' => $c_1_openid,
			'c_1_s1' => $c_1_s1,
			'c_1_fee' => $c_1_fee,
			'c_2_uid' => $c_2_uid,
			'c_2_openid' => $c_2_openid,
			'c_2_s1' => $c_2_s1,
			'c_2_fee' => $c_2_fee,
			'c_3_uid' => $c_3_uid,
			'c_3_openid' => $c_3_openid,
			'c_3_s1' => $c_3_s1,
			'c_3_fee' => $c_3_fee,
			'c_t_uid' => $c_t_uid,
			'c_t_openid' => $c_t_openid,
			'c_t_s1' => $c_t_s1,
			'c_t_fee' => $c_t_fee,
			'c_2t_uid' => $c_2t_uid,
			'c_2t_openid' => $c_2t_openid,
			'c_2t_s1' => $c_2t_s1,
			'c_2t_fee' => $c_2t_fee,
			'c_1h_uid' => $c_1h_uid,
			'c_1h_openid' => $c_1h_openid,
			'c_1h_s1' => $c_1h_s1,
			'c_1h_fee' => $c_1h_fee,
			'c_2h_uid' => $c_2h_uid,
			'c_2h_openid' => $c_2h_openid,
			'c_2h_s1' => $c_2h_s1,
			'c_2h_fee' => $c_2h_fee,
			'c_3h_uid' => $c_3h_uid,
			'c_3h_openid' => $c_3h_openid,
			'c_3h_s1' => $c_3h_s1,
			'c_3h_fee' => $c_3h_fee,
			'c_4h_uid' => $c_4h_uid,
			'c_4h_openid' => $c_4h_openid,
			'c_4h_s1' => $c_4h_s1,
			'c_4h_fee' => $c_4h_fee
		);
		return $fee;
		
	}

	public function html2Array($content)
	{
		$content = preg_replace("/<a href=\"(.*)\" .*>.*<\\/a>/Uis", "--dkLaoLiu--link::pgSuperSplite::\$1--dkLaoLiu--", $content);
		$content = preg_replace("/<img src=\"(.*)\".*\\/>/Uis", "--dkLaoLiu--img::pgSuperSplite::\$1--dkLaoLiu--", $content);
		$content = preg_replace("/<pre.*>(.*)<\\/pre>/Uis", "--dkLaoLiu--pre::pgSuperSplite::\$1--dkLaoLiu--", $content);
		$content = preg_replace("/<strong.*>(.*)<\\/strong>/Uis", "--dkLaoLiu--strong::pgSuperSplite::\$1--dkLaoLiu--", $content);
		$content = preg_replace("/<br.*\\/>/Uis", PHP_EOL, $content);
		$content = preg_replace("/<blockquote>(.*)<\\/blockquote>/Uis", "--dkLaoLiu--quote::pgSuperSplite::\$1--dkLaoLiu--", $content);
		$content = preg_replace("/<hr(.*)\\/>/Uis", "--dkLaoLiu--spline::pgSuperSplite::...--dkLaoLiu--", $content);
		$content = strip_tags($content);
		$content = preg_replace("/(\\t)/Uis", "", $content);
		$content = preg_replace("/&nbsp;/Uis", " ", $content);
		$content = preg_replace("/&.*;/Uis", "", $content);
		$content = explode("--dkLaoLiu--", $content);
		$contentArray = array();
		foreach ($content as $k => $item) {
			$res = str_replace(array("\r\n", "\r", "\n", ""), "", $item);
			if ($res != "") {
				$itemArr = explode("::pgSuperSplite::", $item);
				if (count($itemArr) < 2) {
					$contentArray[] = array("type" => "txt", "content" => trim($itemArr[0]));
					goto label_28;
				}
				$contentArray[] = array("type" => $itemArr[0], "content" => trim($itemArr[1]));
				label_28:
				goto label_176;
			}
			label_176:
		}
		return $contentArray;
	}
	public function province($name)
	{
		$list = array(320000 => "江苏", 640000 => "宁夏", 330000 => "浙江", 410000 => "河南", 650000 => "新疆", 810000 => "香港", 340000 => "安徽", 420000 => "湖北", 500000 => "重庆", 820000 => "澳门", 110000 => "北京", 350000 => "福建", 430000 => "湖南", 510000 => "四川", 120000 => "天津", 360000 => "江西", 440000 => "广东", 520000 => "贵州", 130000 => "河北", 210000 => "辽宁", 370000 => "山东", 450000 => "广西", 530000 => "云南", 610000 => "陕西", 140000 => "山西", 220000 => "吉林", 460000 => "海南", 540000 => "西藏", 620000 => "甘肃", 150000 => "内蒙古", 230000 => "黑龙江", 310000 => "上海", 630000 => "青海", 710000 => "台湾");
		foreach ($list as $key => $val) {
			if (strpos($name, $val) !== false) {
				$provinceId = $key;
				break;
			}
		}
		return $provinceId;
	}

	
	
    
}



